<?php 
header ( "Content-Type: text/html; charset=UTF-8" ); 
include 'HttpClient.class.php';
include_once "DB_Oracle.php";

//定义几个全局变量
$GLOBALS['url'] = $url;
$GLOBALS['a_k'] = $a_k;
$GLOBALS['config'] = $config;
$GLOBALS['resType'] = $resType;
$GLOBALS['flag'] = $flag;
$GLOBALS['cr'] = $cr;
$GLOBALS['Contents']=$Contents;
$GLOBALS['DBsql']=$DBsql;

$DBsql = new DB_Oracle ();
$url='http://search1.mapabc.com/sisserver';//服务参数名称
$a_k="3a1d6aaf05b6a305cacf45848a5889c7403ddeccd7b70d0ac6acd97bffc04132b542857ba8b1302b";//申请的key
$config="RGC";//服务参数名称
$resType="json";//返回结果的数据格式，可选xml或者json
$flag="true";//是否返回明码坐标。
$cr="0";//返回坐标类型0，表示返回坐标和 gridCode（默认） 1，只返回 gridCode 

$act=($_POST['act']) ? $_POST['act'] : $_GET['act'];
if ($act=='single'){//只偏转一个点
	$lng=($_POST['lng']) ? $_POST['lng'] : $_GET['lng'];
	$lat=($_POST['lat']) ? $_POST['lat'] : $_GET['lat'];
	single($lng,$lat);
}else{
	$coors=($_POST['coors']) ? $_POST['coors'] : $_GET['coors'];	
	echo $conten=AllGps($coors);
	$GLOBALS['Contents']=$conten;
	$arr=explode(";",$coors);
	for($i=0;$i<count($arr);$i++){
		$str=explode(",",$arr[$i]);
		GPSTOMAP('lnglat',$str[0],$str[1]);
	}
}

function single($lng,$lat){//使用GET，每次偏转一个点
	$parameter=$GLOBALS['url']."?config=".$GLOBALS['config']."&flag=".$GLOBALS['flag']."&resType=".$GLOBALS['resType']."&cr=".$GLOBALS['cr']."&x1=".$lng."&y1=".$lat."&a_k=".$GLOBALS['a_k'];
	$GLOBALS['Contents']= HttpClient::quickGet($parameter);
	echo $GLOBALS['Contents'];
	GPSTOMAP(lnglat,$lng,$lat)
}
function AllGps($coors){//使用POST，偏转所有的点
	$arr=explode(";",$coors);
	if(count($arr)<=500){
		$content= HttpClient::quickPost($GLOBALS['url'],array(
			'flag'=>$GLOBALS['flag'],
			'config'=>$GLOBALS['config'],
			'resType'=>$GLOBALS['resType'],
			'coors'=>$GLOBALS['coors'],
			'cr'=>$GLOBALS['cr'],
			'a_k'=>$GLOBALS['a_k']
		));
		return $content;
	}else{
		$cc=array();
		$len=ceil(count($arr)/500);
		for($i=0;$i<$len;$i++){
			$coor=array_slice($arr,$i*500,500);
			$lnglats=implode(';',$coor);
			$content[$i]= HttpClient::quickPost($GLOBALS['url'],array(
				'flag'=>$GLOBALS['flag'],
				'config'=>$GLOBALS['config'],
				'resType'=>$GLOBALS['resType'],
				'coors'=>$lnglats,
				'cr'=>$GLOBALS['cr'],
				'a_k'=>$GLOBALS['a_k']
			));
			$str=json_decode($content[$i]);
			$cc=array_merge($cc,$str->list);
		}
		$bb=json_encode($cc);
		$contenJson='{"message":"ok","list":'.$bb.'}';
		return $contenJson;
	}
}
function GPSTOMAP($flag,$lng,$lat){
	if($flag=='lnglat'){
		$content=json_decode($GLOBALS['Contents'],true);
		$lngMap=$content['list'][0]['x'];
		$latMap=$content['list'][0]['y'];
		$sqllng="insert into loc_lng(loc_lng_id,loc_lng_gps,loc_lng_map)values(LOC_LNG_ID_SEQ.NEXTVAL,'$lngMap','$lng')";
  	$sqllat="insert into loc_lat(loc_lat_id,loc_lat_gps,loc_lat_map)values(LOC_LAT_ID_SEQ.Nextval,'$latMap','$lat')";
		$rowlng=$GLOBALS['DBsql']->query ( $sqllng );
		$rowLat=$GLOBALS['DBsql']->query ( $sqllat );
	}else if($flag=='lng'){
		$sqllng="insert into loc_lng(loc_lng_id,loc_lng_gps,loc_lng_map)values(LOC_LNG_ID_SEQ.NEXTVAL,'$lngMap','$lng')";
		$rowlng=$GLOBALS['DBsql']->query ( $sqllng );
	}else if($flag=='lat'){
		$sqllat="insert into loc_lat(loc_lat_id,loc_lat_gps,loc_lat_map)values(LOC_LAT_ID_SEQ.Nextval,'$latMap','$lat')";
		$rowLat=$GLOBALS['DBsql']->query ( $sqllat );
	}
}
?>